package com.chineseall.rebirth.bridge.service;

import java.sql.Date;
import java.util.Collection;
import java.util.Map;

import javax.annotation.Resource;

import jmind.core.cache.MemCache;
import jmind.core.cache.support.XMemCache;
import jmind.core.cache.xmemcached.Memcached;
import jmind.core.util.ComplexTimeUtil;
import jmind.core.util.RegexUtil;

import org.springframework.stereotype.Service;

import com.chineseall.rebirth.api.RebirthApiFactory;
import com.chineseall.rebirth.api.pojo.User;
import com.chineseall.rebirth.api.pojo.Useraccount;
import com.chineseall.rebirth.api.service.IImageService;
import com.chineseall.rebirth.api.service.IUserService;
import com.chineseall.rebirth.bridge.dao.BridgeSqls;
import com.chineseall.rebirth.bridge.dao.ChineseDao;
import com.chineseall.rebirth.bridge.util.XMemcached;
import com.google.common.collect.Maps;

/**
 * 用户聚合服务
 * @author wbxie
 * 2014-4-2
 */
@Service
public class UserAggregationService {
    @Resource
    private ChineseDao chineseDao;

    @Resource
    private ZedisService zedisService;

    public final MemCache<Integer, Map<String, Object>> viewCache = new XMemCache<Integer, Map<String, Object>>(
            XMemcached.getCache(), "views_", Memcached.DAY);

    /**
     * 是否作者
     * 2013-7-24 
     * @param uid
     * @return
     */
    public boolean isAuthor(int uid) {
        return chineseDao.findForInt(BridgeSqls.GET_AUTHOR, uid) > 0;
    }

    public boolean isMonthly(Integer uid) {
        Date endTime = chineseDao.findColumValue(BridgeSqls.IS_MONTHLY, Date.class, uid);
        if (endTime != null) {
            return ComplexTimeUtil.getDayInt(endTime) >= ComplexTimeUtil.getDayInt(System.currentTimeMillis());
        }
        return false;
    }

    /**
     * 粉丝等级
     * 2014-3-28 
     * @param uid 17k id
     * @param bookId 书id
     * @return
     */
    public Map<String, Object> getFans(int bookId, int uid) {
        String key = XMemcached.getFansKey(bookId, uid);
        Map<String, Object> map = XMemcached.getCache().get(key);
        if (map == null) {
            map = chineseDao.findForMap(BridgeSqls.FANS_LEVEL, uid, bookId);
            if (map == null)
                map = Maps.newHashMap();
            XMemcached.getCache().set(key, Memcached.MINUTE * 15, map);

        }
        return map;
    }

    /**
     * 用户聚合的一些信息，用于评论展示
     * 2014-3-28 
     * @param cids 用户中心id
     * @return
     */
    public Map<Integer, Map<String, Object>> getViews(Collection<Integer> cids) {
        IUserService userService = RebirthApiFactory.getFactory().getApiService(IUserService.class);
        IImageService imageService = RebirthApiFactory.getFactory().getApiService(IImageService.class);
        Map<Integer, Map<String, Object>> map = viewCache.getMulti(cids);
        if (map.size() < cids.size()) {
            cids.removeAll(map.keySet());
            Collection<User> users = userService.gets(cids).values();
            Map<String, Object> view = null;
            for (User user : users) {
                view = Maps.newHashMap();
                int cid = user.getCid();
                view.put("userId", user.getUid());
                String nickName = user.getNickName();
                if (RegexUtil.isMobile(nickName)) {
                    nickName = nickName.substring(0, 6) + "****";
                }
                view.put("nikeName", nickName);
                view.put("headImgUrl", imageService.getAvatar(user.getImgId(), 50));
                Useraccount account = zedisService.getAccount(cid);
                int level = account == null ? 0 : account.getVipLevel();
                view.put("vipLevel", level);
                view.put("isSubMonth", isMonthly(user.getUid()) ? 1 : 0);
                viewCache.set(cid, view);
                map.put(cid, view);
            }

        }

        return map;

    }
}
